/**
 * 生成剩余4个题型的静态题目
 */

const fs = require('fs');
const path = require('path');

const ROOT = path.join(__dirname, '..', '..');

function s(str) {
  return JSON.stringify(String(str)).slice(1, -1);
}

// 实际应用题目生成
function generate实际应用() {
  const questions = [];
  
  // 1-15题：购物问题
  for (let i = 1; i <= 15; i++) {
    const price = 5 + i;
    const count = 3 + i;
    const total = price * count;
    questions.push({
      stem: `小明买了${count}个苹果，每个${price}元，一共花了多少钱？`,
      difficulty: 1,
      answer: [total],
      hint1: `总价 = 单价 × 数量`,
      hint2: `${price} × ${count}`,
      solution: `【解题】\n总价 = ${price} × ${count} = ${total}元\n\n答：一共花了${total}元。`
    });
  }
  
  // 16-30题：行程问题
  for (let i = 1; i <= 15; i++) {
    const speed = 50 + i * 5;
    const time = 2 + i * 0.5;
    const distance = speed * time;
    questions.push({
      stem: `一辆汽车以${speed}千米/小时的速度行驶了${time}小时，行驶了多少千米？`,
      difficulty: 2,
      answer: [distance],
      hint1: `距离 = 速度 × 时间`,
      hint2: `${speed} × ${time}`,
      solution: `【解题】\n距离 = ${speed} × ${time} = ${distance}千米\n\n答：行驶了${distance}千米。`
    });
  }
  
  // 31-40题：工程问题
  for (let i = 1; i <= 10; i++) {
    const days = 10 + i;
    const rate = 1 / days;
    const workDays = 5 + i;
    const work = rate * workDays;
    questions.push({
      stem: `一项工程，甲单独做需要${days}天完成，甲做了${workDays}天，完成了这项工程的几分之几？`,
      difficulty: 2,
      answer: [work.toFixed(2)],
      hint1: `工作效率 = 1 / ${days}`,
      hint2: `完成量 = 效率 × 时间`,
      solution: `【解题】\n甲的工作效率 = 1/${days}\n完成量 = 1/${days} × ${workDays} = ${work.toFixed(2)}\n\n答：完成了${work.toFixed(2)}。`
    });
  }
  
  // 41-50题：利润问题
  for (let i = 1; i <= 10; i++) {
    const cost = 50 + i * 10;
    const price = cost + 20 + i * 5;
    const profit = price - cost;
    questions.push({
      stem: `某商品成本${cost}元，售价${price}元，利润是多少元？`,
      difficulty: 2,
      answer: [profit],
      hint1: `利润 = 售价 - 成本`,
      hint2: `${price} - ${cost}`,
      solution: `【解题】\n利润 = ${price} - ${cost} = ${profit}元\n\n答：利润是${profit}元。`
    });
  }
  
  return questions;
}

// 数学建模题目生成
function generate数学建模() {
  const questions = [];
  
  // 1-15题：线性模型
  for (let i = 1; i <= 15; i++) {
    const a = 2 + i;
    const b = 10 + i * 2;
    const x = 5 + i;
    const y = a * x + b;
    questions.push({
      stem: `某商品的销售量y（件）与价格x（元）的关系为y = ${a}x + ${b}，当价格为${x}元时，销售量是多少件？`,
      difficulty: 2,
      answer: [y],
      hint1: `代入x = ${x}`,
      hint2: `y = ${a} × ${x} + ${b}`,
      solution: `【解题】\ny = ${a} × ${x} + ${b} = ${y}件\n\n答：销售量是${y}件。`
    });
  }
  
  // 16-30题：二次函数模型
  for (let i = 1; i <= 15; i++) {
    const a = 1 + i * 0.5;
    const b = -(2 + i);
    const c = 10 + i * 3;
    const x = 2;
    const y = a * x * x + b * x + c;
    questions.push({
      stem: `某物体的高度h（米）与时间t（秒）的关系为h = ${a}t² ${b}t + ${c}，当t = ${x}秒时，高度是多少米？`,
      difficulty: 3,
      answer: [y.toFixed(1)],
      hint1: `代入t = ${x}`,
      hint2: `h = ${a} × ${x}² ${b} × ${x} + ${c}`,
      solution: `【解题】\nh = ${a} × ${x}² ${b} × ${x} + ${c}\n= ${a * x * x} ${b * x} + ${c}\n= ${y.toFixed(1)}米\n\n答：高度是${y.toFixed(1)}米。`
    });
  }
  
  // 31-40题：指数增长模型
  for (let i = 1; i <= 10; i++) {
    const initial = 100 + i * 10;
    const rate = 0.1 + i * 0.01;
    const years = 2 + i;
    const final = initial * Math.pow(1 + rate, years);
    questions.push({
      stem: `某种细菌初始数量为${initial}个，每年增长${(rate * 100).toFixed(0)}%，${years}年后数量约为多少个？`,
      difficulty: 3,
      answer: [Math.round(final)],
      hint1: `指数增长公式：N = N₀(1 + r)ᵗ`,
      hint2: `N = ${initial} × (1 + ${rate})^${years}`,
      solution: `【解题】\nN = ${initial} × (1 + ${rate})^${years}\n≈ ${Math.round(final)}个\n\n答：约为${Math.round(final)}个。`
    });
  }
  
  // 41-50题：成本模型
  for (let i = 1; i <= 10; i++) {
    const fixed = 1000 + i * 100;
    const variable = 10 + i * 2;
    const quantity = 50 + i * 10;
    const total = fixed + variable * quantity;
    questions.push({
      stem: `某工厂生产产品的固定成本为${fixed}元，每件产品的变动成本为${variable}元，生产${quantity}件产品的总成本是多少元？`,
      difficulty: 2,
      answer: [total],
      hint1: `总成本 = 固定成本 + 变动成本 × 数量`,
      hint2: `${fixed} + ${variable} × ${quantity}`,
      solution: `【解题】\n总成本 = ${fixed} + ${variable} × ${quantity}\n= ${fixed} + ${variable * quantity}\n= ${total}元\n\n答：总成本是${total}元。`
    });
  }
  
  return questions;
}

// 竞赛基础题目生成
function generate竞赛基础() {
  const questions = [];
  
  // 1-10题：整数问题
  for (let i = 1; i <= 10; i++) {
    const n = 10 + i * 5;
    const sum = (n * (n + 1)) / 2;
    questions.push({
      stem: `求1 + 2 + 3 + ... + ${n}的值。`,
      difficulty: 2,
      answer: [sum],
      hint1: `等差数列求和公式`,
      hint2: `S = n(n+1)/2`,
      solution: `【解题】\nS = ${n} × (${n} + 1) / 2\n= ${n} × ${n + 1} / 2\n= ${sum}\n\n答：${sum}。`
    });
  }
  
  // 11-20题：数论问题
  const primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
  for (let i = 0; i < 10; i++) {
    const p = primes[i];
    const nextP = primes[i + 1] || 31;
    questions.push({
      stem: `${p}是质数吗？如果是，写"是"；如果不是，写"否"。`,
      difficulty: 1,
      answer: ["是"],
      hint1: `质数只能被1和自己整除`,
      hint2: `${p}的因数只有1和${p}`,
      solution: `【解题】\n${p}只能被1和${p}整除\n∴ ${p}是质数\n\n答：是。`
    });
  }
  
  // 21-30题：不等式问题
  for (let i = 1; i <= 10; i++) {
    const a = 2 + i;
    const b = 10 + i * 3;
    const x = (b - 5) / a;
    questions.push({
      stem: `解不等式：${a}x + 5 < ${b}`,
      difficulty: 2,
      answer: [`x<${x.toFixed(1)}`],
      hint1: `移项`,
      hint2: `${a}x < ${b - 5}`,
      solution: `【解题】\n${a}x + 5 < ${b}\n${a}x < ${b - 5}\nx < ${x.toFixed(1)}\n\n答：x < ${x.toFixed(1)}。`
    });
  }
  
  // 31-40题：几何竞赛题
  for (let i = 1; i <= 10; i++) {
    const a = 3 + i;
    const b = 4 + i;
    const c = Math.sqrt(a * a + b * b);
    questions.push({
      stem: `直角三角形两直角边分别为${a}和${b}，求斜边长。`,
      difficulty: 2,
      answer: [c.toFixed(2)],
      hint1: `勾股定理`,
      hint2: `c² = ${a}² + ${b}²`,
      solution: `【解题】\nc² = ${a}² + ${b}²\n= ${a * a} + ${b * b}\n= ${a * a + b * b}\nc = ${c.toFixed(2)}\n\n答：斜边长为${c.toFixed(2)}。`
    });
  }
  
  // 41-50题：综合竞赛题
  for (let i = 1; i <= 10; i++) {
    const n = 5 + i;
    const sum = n * n;
    questions.push({
      stem: `求前${n}个奇数的和：1 + 3 + 5 + ... + ${2 * n - 1}`,
      difficulty: 3,
      answer: [sum],
      hint1: `前n个奇数的和 = n²`,
      hint2: `${n}²`,
      solution: `【解题】\n前${n}个奇数的和 = ${n}²\n= ${sum}\n\n答：${sum}。`
    });
  }
  
  return questions;
}

// 综合压轴题生成
function generate综合压轴题() {
  const questions = [];
  
  // 1-10题：代数几何综合
  for (let i = 1; i <= 10; i++) {
    const a = 2 + i;
    const b = 3 + i;
    const area = a * b;
    questions.push({
      stem: `矩形的长为${a}，宽为${b}，求面积。`,
      difficulty: 2,
      answer: [area],
      hint1: `面积 = 长 × 宽`,
      hint2: `${a} × ${b}`,
      solution: `【解题】\n面积 = ${a} × ${b} = ${area}\n\n答：面积为${area}。`
    });
  }
  
  // 11-25题：函数与几何综合
  for (let i = 1; i <= 15; i++) {
    const a = 1 + i * 0.5;
    const b = 2 + i;
    const x = 2;
    const y = a * x + b;
    questions.push({
      stem: `直线y = ${a}x + ${b}，当x = ${x}时，y的值是多少？`,
      difficulty: 2,
      answer: [y],
      hint1: `代入x = ${x}`,
      hint2: `y = ${a} × ${x} + ${b}`,
      solution: `【解题】\ny = ${a} × ${x} + ${b}\n= ${a * x} + ${b}\n= ${y}\n\n答：y = ${y}。`
    });
  }
  
  // 26-40题：动点问题综合
  for (let i = 1; i <= 15; i++) {
    const v = 5 + i;
    const t = 3 + i * 0.5;
    const s = v * t;
    questions.push({
      stem: `点P从原点出发，以${v}米/秒的速度沿直线运动，${t}秒后距离原点多远？`,
      difficulty: 2,
      answer: [s],
      hint1: `距离 = 速度 × 时间`,
      hint2: `${v} × ${t}`,
      solution: `【解题】\n距离 = ${v} × ${t} = ${s}米\n\n答：距离原点${s}米。`
    });
  }
  
  // 41-50题：最高难度综合题
  for (let i = 1; i <= 10; i++) {
    const a = 2 + i;
    const b = 3 + i;
    const c = 4 + i;
    const sum = a + b + c;
    questions.push({
      stem: `三角形三边长分别为${a}、${b}、${c}，求周长。`,
      difficulty: 3,
      answer: [sum],
      hint1: `周长 = 三边之和`,
      hint2: `${a} + ${b} + ${c}`,
      solution: `【解题】\n周长 = ${a} + ${b} + ${c} = ${sum}\n\n答：周长为${sum}。`
    });
  }
  
  return questions;
}

function writeFile(filename, questions) {
  const topicName = filename.replace('.ts', '');
  let content = `// ${topicName} - 初中数学综合题型（50题）\n`;
  content += `// 已从动态生成转换为静态题目，确保准确性\n\n`;
  content += `interface Question {\n`;
  content += `  stem: string;\n`;
  content += `  difficulty: number;\n`;
  content += `  answer: (string | number)[];\n`;
  content += `  hint1: string;\n`;
  content += `  hint2: string;\n`;
  content += `  solution: string;\n`;
  content += `}\n\n`;
  content += `export const ${topicName}_QUESTIONS: Question[] = [\n`;
  
  questions.forEach((q, i) => {
    const answerStr = `[${q.answer.map(a => typeof a === 'number' ? a : `"${s(a)}"`).join(', ')}]`;
    content += `  {\n`;
    content += `    stem: "${s(q.stem)}",\n`;
    content += `    difficulty: ${q.difficulty},\n`;
    content += `    answer: ${answerStr},\n`;
    content += `    hint1: "${s(q.hint1)}",\n`;
    content += `    hint2: "${s(q.hint2)}",\n`;
    content += `    solution: "${s(q.solution)}"\n`;
    content += `  }${i < questions.length - 1 ? ',' : ''}\n`;
  });
  
  content += `];\n\n`;
  content += `export default ${topicName}_QUESTIONS;\n`;
  
  const fullPath = path.join(ROOT, 'questions', 'middle-school', filename);
  fs.writeFileSync(fullPath, content, 'utf-8');
  console.log(`✅ 已生成: ${filename} (${questions.length}题)`);
}

function main() {
  console.log('🚀 开始生成剩余4个题型的静态题目...\n');
  
  writeFile('实际应用.ts', generate实际应用());
  writeFile('数学建模.ts', generate数学建模());
  writeFile('竞赛基础.ts', generate竞赛基础());
  writeFile('综合压轴题.ts', generate综合压轴题());
  
  console.log('\n✅ 全部完成！');
  console.log('📊 总计：4个文件，200题');
}

main();

